package com.pro_one.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pro_one.dao.ExerciseDao;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.pojo.Exercise;
import com.pro_one.service.ExerciseService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service(interfaceClass = ExerciseService.class )
@Transactional
public class ExerciseServiceImpl implements ExerciseService {
    @Autowired
    private ExerciseDao exerciseDao;
    @Override
    public void upload(List<Exercise> list) {
        if (list!=null&&list.size()>0){
            for (Exercise exercise : list) {
            this.addexercis(exercise);
            }
        }
    }
/*分页*/
    @Override
    public PageResult findByCondition(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<Exercise> page = exerciseDao.findByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }
/*增加*/
    @Override
    public void add(Exercise exercise) {
        this.addexercis(exercise);
    }
    /*增加的方法*/
     public void addexercis(Exercise exercise){
         /*查询是否已经存在*/
         Integer count = exerciseDao.findname(exercise.getName());
         if (count>0){
             /*如果存在救修改*/
             exerciseDao.modification(exercise);
         }
         exerciseDao.upload(exercise);
     }
}
